import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { ShipperBaseService } from 'src/app/shared/services/business/shipper-base.service';
import { AccountManagemantService } from '../../services/account-managemant.service';

@Component({
  selector: 'app-partner-account-management-virtual-account-detail',
  templateUrl: './virtual-account-detail.component.html',
})
export class PartnerAccountManagementVirtualAccountDetailComponent implements OnInit {
  url = `/user`;
  schema!: SFSchema;
  ui!: SFUISchema;
  @ViewChild('st') private readonly st!: STComponent;
  @ViewChild('sf') private readonly sf!: SFComponent;
  columns: STColumn[] = [];
  roleId = '';
  _$expand = false;
  record = {};

  constructor(public shipperservice: ShipperBaseService, public service: AccountManagemantService,
    private modalRef: NzModalRef, public router: Router) {
  }

  get reqParams() {
    return { ...this.sf?.value, roleId: this.roleId };
  }
  ngOnInit(): void {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        _$expand: {
          type: 'boolean', ui: { hidden: true }
        },
        userName: {
          title: '合伙人名称',
          type: 'string',
          ui: {
            placeholder: '请输入',
          },
        },
        phone: {
          title: '手机号',
          type: 'string',
          ui: {
            placeholder: '请输入',
          },
        },
        ltdId: {
          title: '网络货运人',
          type: 'string',
          default: '',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.shipperservice.getNetworkFreightForwarder({}, true)
          }

        },
        bankType: {
          type: 'string',
          title: '银行类型',
          default: '',
          ui: {
            widget: 'dict-select',
            params: {
              dictKey: 'bankname:type'
            },
            placeholder: '请选择',
            allowClear: true,
            containsAllLabel: true,
            visibleIf: {
              _$expand: (value: boolean) => value,
            },
          },
        },
        virtualAccount: {
          title: '虚拟账户',
          type: 'string',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              _$expand: (value: boolean) => value
            },
          },
        },
        createTime: {
          type: 'string',
          title: '创建时间',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            autoComplete: 'off',
            format: 'yyyy-MM-dd',
            visibleIf: {
              _$expand: (value: boolean) => value
            },
          } as SFDateWidgetSchema,
        },
      }
    }
    this.ui = { '*': { spanLabelFixed: 100, grid: { span: 8, gutter: 4 } }, };
  }

  /**
* 初始化数据列表
*/
  initST() {
    this.columns = [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return (this.st.pi * this.st.ps) - this.st.ps + idx + 1 + ''
        }
      },
      { title: '合伙人', index: 'name', className: 'text-center', width: 200 },
      { title: '手机号', index: 'phone', className: 'text-center', width: 150 },
      { title: '网络货运人', index: 'ltdName', className: 'text-center', width: 200 },
      { title: '银行类型', index: 'bankTypeLabel', className: 'text-center', width: 120 },
      { title: '虚拟账户', index: 'virtualAccount', className: 'text-center', width: 180 },
      { title: '可用余额', render: 'availableBalance', className: 'text-center', width: 180 },
      { title: '账户总余额', render: 'allBalance', className: 'text-center', width: 180 },
      { title: '创建时间', index: 'createTime', className: 'text-center', width: 200 },
      { title: '状态', index: 'stateDeletedLabel', className: 'text-center', width: 120 },
      {
        title: '操作',
        width: 120,
        buttons: [
          {
            text: '查看明细',
            click: (_record) => this.viewDetail(_record)
          }
        ]
      }
    ];
  }

  resetSF() {
    this._$expand = false;
    this.sf.reset();
    setTimeout(() => {
      this.st.reset();
    })
  }
  /**
* 伸缩查询条件
*/
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }

  add(): void {
    // this.modal
    //   .createStatic(FormEditComponent, { i: { id: 0 } })
    //   .subscribe(() => this.st.reload());
  }
  search() {
    this.st.load(1);
  }
  export() { }
  /**
   *
   * @param _record 当前行信息
   */
  viewDetail(_record: any) {
    // this.router.navigate([`/partner/account-management/am/detail/${_record?.roleId}`], {
    //   queryParams: {
    //     channelSource: _record?.accountType,
    //     bankType: _record?.bankType,
    //     ltdId: _record?.ltdId
    //   }
    // });
    window.open(location.origin + `/#/partner/account-management/am/detail/${_record?.roleId}?ltdId=${_record?.ltdId}&channelSource=${_record?.accountType}&bankType=${_record?.bankType}`);
  }

  close() {
    this.modalRef.destroy();
  }

}
